<?xml version='1.0' encoding='UTF-8'?>



<!--
 * csdl.xsl
 * 
 * AUTHOR:
 * Zlatko Michailov (http://blogs.msdn.com/esql)
 * 
 * PURPOSE:
 * Transformation of Entity Framework CSDL format into HTML.
 * 
 * COPYRIGHT:
 * (c) Microsoft Corp., 2006-2007
-->



<xsl:stylesheet version='1.0'
	xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
	xmlns:edm='http://schemas.microsoft.com/ado/2006/04/edm'>


  <xsl:template match='/'>
    <html>
      <head>
        <style>
          body, table
          {
          font-family: Verdana;
          font-size: 10pt;
          color: black;
          background: white;
          }

          .set
          {
          color: #008000;
          }

          .object
          {
          font-weight: bold;
          background: #c0c0c0;
          }

          .abstract
          {
          font-style: italic;
          background: #c0c0c0;
          }

          .base
          {
          background: #c0c0c0;
          }

          .key
          {
          color: maroon;
          font-weight: bold;
          }

          .navigation
          {
          color: blue;
          font-style: italic;
          }
        </style>
      </head>
      <body>
        <xsl:apply-templates select='edm:Schema' />
      </body>
    </html>
  </xsl:template>


  <xsl:template match='edm:Schema'>
    <h3>
      <xsl:value-of select='@Namespace' />
    </h3>
    <a href='#Entity Sets' >Entity Sets</a>
    <br />
    <a href='#Entity Types'>Entity Types</a>
    <br />
    <a href='#Complex Types' >Complex Types</a>
    <br />
    <a href='#Associations' >Associations</a>
    <br />
    <br />
    <br />


    <br />
    <a name='Entity Sets' />
    <hr />
    <h4>Entity Sets</h4>
    <xsl:apply-templates select='edm:EntityContainer' />
    
    <br />
    <a name='Entity Types' />
    <hr />
    <h4>Entity Types</h4>
    <xsl:apply-templates select='edm:EntityType' />

    <br />
    <a name='Complex Types' />
    <hr />
    <h4>Complex Types</h4>
    <xsl:apply-templates select='edm:ComplexType' />

    <br />
    <a name='Associations' />
    <hr />
    <h4>Associations</h4>
    <xsl:apply-templates select='edm:Association' />
    
    <br />
    <br />
    <br />
    <br />
    <br />
  </xsl:template>


  <xsl:template match='edm:EntityContainer'>
    <table border='1'>
      <tr>
        <td colspan='2' class='object'>
          <xsl:value-of select='@Name' />
        </td>
      </tr>
      <xsl:apply-templates select='edm:EntitySet' />
    </table>
    <br />
    <br />
  </xsl:template>


  <xsl:template match='edm:EntitySet'>
    <tr>
      <td class='set'>
        <xsl:value-of select='@Name' />
      </td>
      <td>
        <xsl:text disable-output-escaping='yes'>
      &lt;a href='#</xsl:text>
        <xsl:value-of select='@EntityType' />
        <xsl:text disable-output-escaping='yes'>' &gt;</xsl:text>
        <xsl:value-of select='@EntityType' />
        <xsl:text disable-output-escaping='yes'>
      &lt;/a&gt;
    </xsl:text>
      </td>
    </tr>
  </xsl:template>


  <xsl:template match='edm:EntityType|edm:ComplexType'>
    <xsl:text disable-output-escaping='yes'>
      &lt;a name='</xsl:text>
    <xsl:value-of select='../@Namespace' />.<xsl:value-of select='@Name' />
    <xsl:text disable-output-escaping='yes'>' /&gt;</xsl:text>

    <table border='1'>
      <tr>
        <xsl:text disable-output-escaping='yes'>
          &lt;td colspan='2' class=</xsl:text>
          <xsl:if test='not(boolean(@Abstract))'>'object'</xsl:if>
          <xsl:if test='boolean(@Abstract)'>'abstract'</xsl:if>
        <xsl:text disable-output-escaping='yes'>&gt;</xsl:text>
          <xsl:value-of select='@Name' />
        <xsl:text disable-output-escaping='yes'>&lt;/td&gt;</xsl:text>
      </tr>
      
      <xsl:if test='string-length(@BaseType)>0' >
        <tr>
          <td colspan='2' class='base'>
            <xsl:text disable-output-escaping='yes'>
              &lt;a href='#</xsl:text>
            <xsl:value-of select='@BaseType' />
            <xsl:text disable-output-escaping='yes'>' &gt;</xsl:text>
              <xsl:value-of select='@BaseType' />
            <xsl:text disable-output-escaping='yes'>&lt;/a&gt;</xsl:text>
          </td>
        </tr>
      </xsl:if>

      <xsl:apply-templates select='edm:Property' />
      <xsl:apply-templates select='edm:NavigationProperty' />
    </table>
    <br />
    <br />
  </xsl:template>


  <xsl:template match='edm:Property'>
    <tr>
      <xsl:variable name='propertyName' select='@Name' />
      <xsl:variable name='isKey' select='../edm:Key/edm:PropertyRef[@Name=$propertyName]' />
      <xsl:choose>
        <xsl:when test='$isKey' >
          <td class='key'>
            <xsl:value-of select='@Name' />
          </td>
          <td class='key'>
            <xsl:value-of select='@Type' />
          </td>
        </xsl:when>

        <xsl:when test='not($isKey)' >
          <td>
            <xsl:value-of select='@Name' />
          </td>
          <td>
            <xsl:if test='starts-with(@Type,../../@Namespace)' >
              <xsl:text disable-output-escaping='yes'>
              &lt;a href='#</xsl:text>
            </xsl:if>
            <xsl:value-of select='@Type' />
            <xsl:if test='starts-with(@Type,../../@Namespace)' >
              <xsl:text disable-output-escaping='yes'>' &gt;</xsl:text>
              <xsl:value-of select='@Type' />
              <xsl:text disable-output-escaping='yes'>&lt;/a&gt;</xsl:text>
            </xsl:if>
          </td>
        </xsl:when>
      </xsl:choose>
    </tr>
  </xsl:template>


  <xsl:template match='edm:NavigationProperty'>
    <tr>
      <td class='navigation'>
        <xsl:value-of select='@Name' />
      </td>
      <td>
        <xsl:text disable-output-escaping='yes'>
              &lt;a href='#</xsl:text>
        <xsl:value-of select='@Relationship' />
        <xsl:text disable-output-escaping='yes'>' &gt;</xsl:text>
        <xsl:value-of select='@Relationship' />
        <xsl:text disable-output-escaping='yes'>&lt;/a&gt;</xsl:text>
      </td>
    </tr>
  </xsl:template>


  <xsl:template match='edm:Association'>
    <xsl:text disable-output-escaping='yes'>
      &lt;a name='</xsl:text>
    <xsl:value-of select='../@Namespace' />.<xsl:value-of select='@Name' />
    <xsl:text disable-output-escaping='yes'>' /&gt;</xsl:text>

    <table border='1'>
      <tr>
        <td colspan='3' class='object'>
          <xsl:value-of select='@Name' />
        </td>
      </tr>
      <xsl:apply-templates select='edm:End' />
    </table>
    <br />
    <br />
  </xsl:template>


  <xsl:template match='edm:End'>
    <tr>
      <td class='navigation'>
        <xsl:value-of select='@Role' />
      </td>
      <td>
        <xsl:text disable-output-escaping='yes'>
              &lt;a href='#</xsl:text>
        <xsl:value-of select='@Type' />
        <xsl:text disable-output-escaping='yes'>' &gt;</xsl:text>
        <xsl:value-of select='@Type' />
        <xsl:text disable-output-escaping='yes'>&lt;/a&gt;</xsl:text>
      </td>
      <td>
        <xsl:value-of select='@Multiplicity' />
      </td>
    </tr>
  </xsl:template>


</xsl:stylesheet>
